// pages/course/detail.js

function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

import { get, getWithPath } from '../../utils/http'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    danmuList:[],
    sendValue: "",
    changeValue: "",
    thumbNums: 0,
    isPraise:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.loadData(options.id);
      return
    }
    const event =  this.getOpenerEventChannel()
    const that = this
    event.on("courseData" , function(data) {
      that.loadInit(data)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  loadInit(data){
    const that = this
    const wec = wx
    console.log(data)
    const detail = data
    that.setData({detail:detail})
    wec.setNavigationBarTitle({
      title: detail.name,
      fail: (err)=>{
        console.log(err)
      }
    })
    that.videoCtx = wec.createVideoContext('video_'+detail.id)
    that.getLikeStatus({targetType:"course",id:detail.id})
    that.submit = false
  },

  onChange(e){
    this.setData({
      changeValue:e.detail.value
    })
  },
  onConfirm(e){
    this.sendDanmu(e.detail.value)
  },
  onSend(){
    this.sendDanmu(this.data.changeValue)
  },
  sendDanmu(str){
    if(!str){
      return;
    }
    if(!this.videoCtx){
      this.videoCtx = wx.createVideoContext('video_'+detail.id)
    }

    if(this.videoCtx){
      const color = getRandomColor()
      console.log("发送弹幕:" , str , color)
      this.videoCtx.sendDanmu({
        text: str,
        color: color
      })
      this.setData({
        sendValue:"",
        changeValue: ""
      })
    }
  },

  onLike(){
    if(this.submit){
      this.setData({
        thumbNums: 2,
      })
      return
    }
    this.submit = true
    const likeStatus = !this.data.isPraise
    const {id} = this.data.detail
    const that = this
    get("/user/like",{targetType:"course",id:id , likeStatus: likeStatus?1:0})
    .then(res=>{
      setTimeout(() => {
        that.setData({
          thumbNums: 2,
          isPraise: likeStatus,
        })
        that.submit = false
      }, 1000);
    }).catch(err => {
      wx.showToast({
        title: '网络异常,请稍后再试',
        icon: 'none'
      })
      that.submit = false
    })
  },

  getLikeStatus(opt){
    const that = this
    get("/user/getLikeStatus",opt)
    .then(res=>{
      that.setData({
        isPraise: res.data && res.data == 1
      })
    }).catch(err => {
      wx.showToast({
        title: '网络异常,请稍后再试',
        icon: 'none'
      })
    })
  },

  loadData(id){
    getWithPath("/user/course/{id}",{id:id})
    .then(res=>{
      const data = res.data || {}
      if(data.coverImage){
        if(data.coverImage.includes(",")){
          data.coverImage = data.coverImage.split(",")
        }else{
          data.coverImage = [data.coverImage]
        }
      }
      this.loadInit(data)
    }).catch(err=>{
      wx.showToast({
        title: '网络异常,请稍后再试',
        icon: 'none'
      })
      this.submit = false
    })
  }
})